package cfg

import (
	"github.com/twgh/xcgui/widget"
)

type comboBoxUI struct {
	combo *widget.ComboBox
}

type EVENT_COMBO_SEL func(*comboBoxUI, int, *bool)

type GpioCfgItem struct {
	body *widget.LayoutEle
	cb1  *comboBoxUI
	cb2  *comboBoxUI
	cb3  *comboBoxUI
	cb4  *comboBoxUI
	cb5  *comboBoxUI
	cb6  *comboBoxUI
}

func (c *GpioCfgItem) GPIO() int {
	return c.cb1.combo.GetSelItem()
}

func (c *GpioCfgItem) Pin() int {
	return c.cb2.combo.GetSelItem()
}

func (c *GpioCfgItem) IsOutput() bool {
	return c.cb3.combo.GetSelItem() == 1
}

func (c *GpioCfgItem) IsPullUp() bool {
	return c.cb4.combo.GetSelItem() == 1
}

func (c *GpioCfgItem) IsPullPush() bool {
	return c.cb4.combo.GetSelItem() == 1
}

func (c *GpioCfgItem) IsInteruptEn() bool {
	return c.cb5.combo.GetSelItem() == 1
}

func (c *GpioCfgItem) IsHighSpeed() bool {
	return c.cb5.combo.GetSelItem() == 1
}

func (c *GpioCfgItem) IsHighLevel() bool {
	return c.cb6.combo.GetSelItem() == 1
}

func (c *GpioCfgItem) GetExtiTriggerType() int {
	return c.cb6.combo.GetSelItem()
}

func (c *GpioCfgItem) Destroy() {
	c.body.Destroy()
}

func (c *GpioCfgItem) SetBaseConfig(gpio int, pin int) {
	c.cb1.combo.SetSelItem(gpio)
	c.cb2.combo.SetSelItem(pin)
}

func (c *GpioCfgItem) SetOutputConfig(isPullPush bool, isHighSpeed bool, isHighLevel bool) {
	c.cb3.combo.SetSelItem(1)
	c.onSelOutputType()
	if isPullPush {
		c.cb4.combo.SetSelItem(1)
	} else {
		c.cb4.combo.SetSelItem(0)
	}
	if isHighSpeed {
		c.cb5.combo.SetSelItem(1)
	} else {
		c.cb5.combo.SetSelItem(0)
	}
	if isHighLevel {
		c.cb6.combo.SetSelItem(1)
	} else {
		c.cb6.combo.SetSelItem(0)
	}
}

func (c *GpioCfgItem) SetInputConfig(isPullUp bool, isInteruptEn bool, extiTriggerType int) {
	if isPullUp {
		c.cb4.combo.SetSelItem(1)
	} else {
		c.cb4.combo.SetSelItem(0)
	}
	if isInteruptEn {
		c.cb5.combo.SetSelItem(1)
	} else {
		c.cb5.combo.SetSelItem(0)
	}
	c.cb6.combo.SetSelItem(extiTriggerType)
}

func (c *GpioCfgItem) onIOTypeSelect(iItem int32, pbHandled *bool) int {
	if iItem == 0 {
		c.onSelInputType()
	} else {
		c.onSelOutputType()
	}
	c.body.Redraw(false)
	return 0
}

func (c *GpioCfgItem) onSelInputType() {
	c.cb4.combo.DeleteItemAll()
	c.cb5.combo.DeleteItemAll()
	c.cb6.combo.DeleteItemAll()
	c.cb4.AddItemText("浮空", "上拉")
	c.cb5.AddItemText("关中断", "开中断")
	c.cb6.AddItemText("默认", "上升沿", "下降沿", "边缘")
}

func (c *GpioCfgItem) onSelOutputType() {
	c.cb4.combo.DeleteItemAll()
	c.cb5.combo.DeleteItemAll()
	c.cb6.combo.DeleteItemAll()
	c.cb4.AddItemText("开漏", "推挽")
	c.cb5.AddItemText("低速", "高速")
	c.cb6.AddItemText("低电平", "高电平")
}

func (c *comboBoxUI) AddItemText(a ...string) {
	index := c.combo.GetSelItem()
	for _, v := range a {
		c.combo.AddItemText(v)
	}
	if index < 0 {
		index = 0
	}
	if index >= len(a) {
		index = 0
	}
	c.combo.SetSelItem(index)
}

func AddComboBox(parent *widget.LayoutEle, id int) *widget.ComboBox {
	combo := widget.NewComboBoxByHandle(parent.GetChildByID(id))
	combo.CreateAdapter()
	combo.EnableEdit(false)
	return combo
}

func AddGpioCfgItemUI(res []byte, parent *widget.LayoutEle, all bool) *GpioCfgItem {
	body := widget.NewLayoutEleByLayoutZipMem(res, "GPIO.xml", "", parent.Handle, 0)
	btn := widget.NewButtonByHandle(body.GetChildByID(7))
	btn.SetText("x")

	cb1 := &comboBoxUI{AddComboBox(body, 1)}
	cb2 := &comboBoxUI{AddComboBox(body, 2)}
	cb6 := &comboBoxUI{AddComboBox(body, 6)}
	if all {
		cb1.AddItemText("默认")
		cb2.AddItemText("默认")
		cb6.AddItemText("低电平", "高电平")
	} else {
		cb1.AddItemText("GPIOA", "GPIOB", "GPIOC", "GPIOD", "GPIOE", "GPIOF", "停用")
		cb2.AddItemText("PIN_0", "PIN_1", "PIN_2", "PIN_3", "PIN_4", "PIN_5", "PIN_6", "PIN_7")
	}

	cb3 := &comboBoxUI{AddComboBox(body, 3)}
	cb3.AddItemText("输入", "输出")

	cb4 := &comboBoxUI{AddComboBox(body, 4)}
	cb5 := &comboBoxUI{AddComboBox(body, 5)}

	parent.AdjustLayout(0)
	parent.Redraw(false)
	cfg := &GpioCfgItem{
		body: body,
		cb1:  cb1,
		cb2:  cb2,
		cb3:  cb3,
		cb4:  cb4,
		cb5:  cb5,
		cb6:  cb6,
	}

	cb3.combo.Event_COMBOBOX_SELECT(cfg.onIOTypeSelect)
	cfg.onSelInputType()

	return cfg
}
